//
//  RegistViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/7/7.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "RegistViewController.h"
#import "UIButton+Timer.h"
@interface RegistViewController ()
{
    NSMutableArray*textArr;
    NSString*validationStr;
}
@property(nonatomic,strong)UITextField*textF;

@end

@implementation RegistViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    textArr=[NSMutableArray new];
    self.view.backgroundColor=LRRGBColor(235, 240, 243);

    self.title=@"注册";
    [self setUI];
}
-(void)setUI
{
    NSArray*arr=@[@"手机号",@"重设密码（6-16位，不包含特殊符号）",@"确认密码",@"输入手机验证码"];
    for (int i=0; i<4; i++) {
        _textF=[[UITextField alloc]initWithFrame:CGRectMake(20, 85+(60*i), width_screen-40, 45)];
        _textF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 1)];
        
        _textF.leftViewMode = UITextFieldViewModeAlways;
        _textF.backgroundColor=[UIColor whiteColor];
        _textF.layer.cornerRadius=5;
        _textF.layer.borderWidth=1;
        _textF.layer.borderColor=LRRGBColor(221, 221, 221).CGColor;
        _textF.placeholder=arr[i];
        [self.view addSubview:_textF];
        [textArr addObject:_textF];
    }
    //倒计时
    UIButton*timerButton=[UIButton buttonWithType:UIButtonTypeCustom];
    timerButton.frame=CGRectMake(width_screen-130, 275, 100, 25);
    timerButton.backgroundColor=[UIColor whiteColor];
    timerButton.layer.cornerRadius=3;
    timerButton.layer.borderWidth=1;
    timerButton.layer.borderColor=LRRGBColor(22, 141, 210).CGColor;
    [timerButton setTitleColor:LRRGBColor(22, 141, 210) forState:0];
    timerButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [timerButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:timerButton];

    //注册按钮
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor=[UIColor orangeColor];
    button.layer.cornerRadius=5;
    
    button.frame=CGRectMake(20, 325, width_screen-40, 45);
    [button setTitle:@"确定" forState:0];
    [button addTarget:self action:@selector(registButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//注册请求
-(void)registRequest
{
    UITextField*nserText=textArr[0];
    UITextField*pwdText=textArr[1];

    [HTTPURL postRequest:LINK_BASE_URL(@"/register/register.api") parameters:@{@"userName":nserText.text,@"password":pwdText.text,@"validationCode":validationStr} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
//验证码
-(void)validationRequest
{
    UITextField*nserText=textArr[0];

    [HTTPURL postRequest:LINK_BASE_URL(@"register/generateValidation.api") parameters:@{@"phone":nserText.text} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        validationStr=responseObject[@"data"];
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
//注册
-(void)registButtonClick
{
    if (_textF.text.length==0) {
        [self promptViewmessage:@"输入框不能为空" sureBlock:^{

        }];
    }else{
        [self registRequest];

    }
}
-(void)promptViewmessage:(NSString *)message sureBlock:(void(^)())sureBlock
{
    UIAlertController * alert= [HTTPURL showOneAlertWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert sureTitle:@"确定" sureBlock:^{
        sureBlock();
    }];
    [self presentViewController:alert animated:YES completion:nil];
    
}
- (void)buttonAction:(UIButton *)button
{  [self validationRequest];
    button.time = 10;
    button.format = @"%ld秒后重试";
    [button startTimer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
